<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Mrp extends MX_Controller {
    
  function __construct() {
    $this->load->library('manimage');
    $this->load->model('frm/mfrm');
    $this->menu = $this->cek();
  }
	
	public function add_inventory($id_mrp_inventory = 0){
    if($this->input->post()){
      $pst = $this->input->post();
      $config['upload_path'] = './files/mrp/inventory/';
      $config['allowed_types'] = 'gif|jpg|png';
//        $config['max_size']	= '100';
      $config['max_width']  = '1168';
      $config['max_height']  = '768';

      $this->load->library('upload', $config);
//        $this->debug($_FILES, true);
      if($_FILES['picture']['name']){
        if ( ! $this->upload->do_upload('picture')){
          $error = array('error' => $this->upload->display_errors());
          print $this->upload->display_errors();
          die;
        }
        else{
          $data = array('upload_data' => $this->upload->data());
          $this->manimage->load('./files/mrp/inventory/'.$data['upload_data']['file_name']); 
          $this->manimage->resizeToWidth(590); 
          $this->manimage->save('./files/mrp/inventory/'.$data['upload_data']['file_name']);

          $this->manimage->load('./files/mrp/inventory/'.$data['upload_data']['file_name']); 
          $this->manimage->resizeToWidth(270); 
          $this->manimage->save('./files/mrp/inventory/270/'.$data['upload_data']['file_name']);

          $this->manimage->load('./files/mrp/inventory/'.$data['upload_data']['file_name']); 
          $this->manimage->resizeToWidth(50); 
          $this->manimage->save('./files/mrp/inventory/50/'.$data['upload_data']['file_name']);
        }
      }
      if($pst['id_detail']){
        $kirim = array(
            'title'           => $pst['title'],
            'sn'              => $pst['sn'],
            'id_mrp_inventory_brand'    => $pst['id_mrp_inventory_brand'],
            'id_mrp_inventory_category'    => $pst['id_mrp_inventory_category'],
            'update_by_users' =>  $this->session->userdata("id"),
        );
        if($data['upload_data']['file_name']){
          $kirim['picture'] = $data['upload_data']['file_name'];
        }
        $id_mrp_inventory = $this->global_models->update("mrp_inventory", array("id_mrp_inventory"=> $pst['id_detail']), $kirim);
      }
      else{
        
        $kirim = array(
            'title'           => $pst['title'],
            'sn'              => $pst['sn'],
            'id_mrp_inventory_brand'    => $pst['id_mrp_inventory_brand'],
            'id_mrp_inventory_category'    => $pst['id_mrp_inventory_category'],
            'create_by_users' =>  $this->session->userdata("id"),
            'create_date'     =>  date("Y-m-d H:i:s")
        );
        if($data['upload_data']['file_name']){
          $kirim['picture'] = $data['upload_data']['file_name'];
        }
        $id_mrp_inventory = $this->global_models->insert("mrp_inventory", $kirim);
      }
      if($id_mrp_inventory){
        redirect("mrp/inventory/1/Berhasil");
      }
      else{
        redirect("mrp/inventory/2/Gagal");
      }
    }
    else{
      $data = $this->global_models->get("mrp_inventory", array("id_mrp_inventory" => $id_mrp_inventory));
      $brand = $this->global_models->get_dropdown("mrp_inventory_brand", "id_mrp_inventory_brand", "name");
      $category = $this->global_models->get_dropdown("mrp_inventory_category", "id_mrp_inventory_category", "name");
      $this->template->build("add-inventory",
        array(
              'url'         => base_url()."themes/".DEFAULTTHEMES."/",
              'menu'        => 'mrp/inventory',
              'title'       => lang("mrp_inventory"),
              'detail'      => $data,
              'brand'       => $brand,
              'category'    => $category
            ));
      $this->template
        ->set_layout('default')
        ->build("add-inventory");
    }
  }
  
	public function add_bank($id_mrp_bank = 0){
    if($this->input->post()){
      $pst = $this->input->post();
      
      if($pst['id_detail']){
        $kirim = array(
            'title'           => $pst['title'],
            'code'            => $pst['code'],
            'update_by_users' =>  $this->session->userdata("id"),
        );
        $id_mrp_bank = $this->global_models->update("mrp_bank", array("id_mrp_bank"=> $pst['id_detail']), $kirim);
      }
      else{
        $kirim = array(
            'title'           => $pst['title'],
            'code'            => $pst['code'],
            'create_by_users' =>  $this->session->userdata("id"),
            'create_date'     =>  date("Y-m-d H:i:s")
        );
        $id_mrp_bank = $this->global_models->insert("mrp_bank", $kirim);
      }
      if($id_mrp_bank){
        $this->session->set_flashdata('success', 'Data tersimpan');
      }
      else{
        $this->session->set_flashdata('notice', 'Data gagal disimpan');
      }
      redirect("mrp/bank");
    }
    else{
      $data = $this->global_models->get("mrp_bank", array("id_mrp_bank" => $id_mrp_bank));
      $this->template->build("add-bank",
        array(
              'url'         => base_url()."themes/".DEFAULTTHEMES."/",
              'menu'        => 'mrp/bank',
              'title'       => lang("mrp_bank"),
              'detail'      => $data,
            ));
      $this->template
        ->set_layout('default')
        ->build("add-bank");
    }
  }
  
	public function add_supplier($id_mrp_supplier = 0){
    if($this->input->post()){
      $pst = $this->input->post();
      
      $pst = $this->input->post();
      $config['upload_path'] = './files/mrp/supplier/';
      $config['allowed_types'] = 'gif|jpg|png';
      $config['max_width']  = '1168';
      $config['max_height']  = '768';

      $this->load->library('upload', $config);
      if($_FILES['picture']['name']){
        if ( ! $this->upload->do_upload('picture')){
          $error = array('error' => $this->upload->display_errors());
          print $this->upload->display_errors();
          die;
        }
        else{
          $data = array('upload_data' => $this->upload->data());
          $this->manimage->load('./files/mrp/supplier/'.$data['upload_data']['file_name']); 
          $this->manimage->resizeToWidth(590); 
          $this->manimage->save('./files/mrp/supplier/'.$data['upload_data']['file_name']);

          $this->manimage->load('./files/mrp/supplier/'.$data['upload_data']['file_name']); 
          $this->manimage->resizeToWidth(270); 
          $this->manimage->save('./files/mrp/supplier/270/'.$data['upload_data']['file_name']);

          $this->manimage->load('./files/mrp/supplier/'.$data['upload_data']['file_name']); 
          $this->manimage->resizeToWidth(50); 
          $this->manimage->save('./files/mrp/supplier/50/'.$data['upload_data']['file_name']);
        }
      }
      
      if($pst['id_detail']){
        $kirim = array(
            'title'           => $pst['title'],
            'id_mrp_bank'     => $pst['id_mrp_bank'],
            'pic'             => $pst['pic'],
            'telp'            => $pst['telp'],
            'email'           => $pst['email'],
            'account_number'  => $pst['account_number'],
            'account_name'    => $pst['account_name'],
            'address'         => $pst['address'],
            'update_by_users' =>  $this->session->userdata("id"),
        );
        if($data['upload_data']['file_name']){
          $kirim['picture'] = $data['upload_data']['file_name'];
        }
        $id_mrp_supplier = $this->global_models->update("mrp_supplier", array("id_mrp_supplier"=> $pst['id_detail']), $kirim);
      }
      else{
        $kirim = array(
            'title'           => $pst['title'],
            'id_mrp_bank'     => $pst['id_mrp_bank'],
            'pic'             => $pst['pic'],
            'telp'            => $pst['telp'],
            'email'           => $pst['email'],
            'account_number'  => $pst['account_number'],
            'account_name'    => $pst['account_name'],
            'address'         => $pst['address'],
            'create_by_users' =>  $this->session->userdata("id"),
            'create_date'     =>  date("Y-m-d H:i:s")
        );
        if($data['upload_data']['file_name']){
          $kirim['picture'] = $data['upload_data']['file_name'];
        }
        $id_mrp_supplier = $this->global_models->insert("mrp_supplier", $kirim);
      }
      if($id_mrp_supplier){
        $this->session->set_flashdata('success', 'Data tersimpan');
      }
      else{
        $this->session->set_flashdata('notice', 'Data gagal disimpan');
      }
      redirect("mrp/supplier");
    }
    else{
      $data = $this->global_models->get("mrp_supplier", array("id_mrp_supplier" => $id_mrp_supplier));
      $bank = $this->global_models->get_dropdown('mrp_bank', "id_mrp_bank", "title");
      $this->template->build("add-supplier",
        array(
              'url'         => base_url()."themes/".DEFAULTTHEMES."/",
              'menu'        => 'mrp/supplier',
              'title'       => lang("mrp_supplier"),
              'detail'      => $data,
              'bank'        => $bank
            ));
      $this->template
        ->set_layout('default')
        ->build("add-supplier");
    }
  }
  
	public function add_brand($id_mrp_inventory_brand = 0){
    if($this->input->post()){
      $pst = $this->input->post();
      
      $pst = $this->input->post();
      
      if($pst['id_detail']){
        $kirim = array(
            'name'            => $pst['name'],
            'nicename'        => $this->global_models->nicename($pst['name']),
            'note'            => $pst['note'],
            'update_by_users' =>  $this->session->userdata("id"),
        );
        $id_mrp_inventory_brand = $this->global_models->update("mrp_inventory_brand", array("id_mrp_inventory_brand"=> $pst['id_detail']), $kirim);
      }
      else{
        $kirim = array(
            'name'            => $pst['name'],
            'nicename'        => $this->global_models->nicename($pst['name']),
            'note'            => $pst['note'],
            'create_by_users' =>  $this->session->userdata("id"),
            'create_date'     =>  date("Y-m-d H:i:s")
        );
        $id_mrp_inventory_brand = $this->global_models->insert("mrp_inventory_brand", $kirim);
      }
      if($id_mrp_inventory_brand){
        $this->session->set_flashdata('success', 'Data tersimpan');
      }
      else{
        $this->session->set_flashdata('notice', 'Data gagal disimpan');
      }
      redirect("mrp/brand");
    }
    else{
      $data = $this->global_models->get("mrp_inventory_brand", array("id_mrp_inventory_brand" => $id_mrp_inventory_brand));
      $this->template->build("add-brand",
        array(
              'url'         => base_url()."themes/".DEFAULTTHEMES."/",
              'menu'        => 'mrp/brand',
              'title'       => lang("mrp_brand"),
              'detail'      => $data,
            ));
      $this->template
        ->set_layout('default')
        ->build("add-brand");
    }
  }
  
	public function add_asset_category($id_mrp_inventory_category = 0){
    if($this->input->post()){
      $pst = $this->input->post();
      
      if($pst['id_detail']){
        $kirim = array(
            'id_frm_account'  => $pst['id_frm_account'],
            'id_frm_account_penyusutan'  => $pst['id_frm_account_penyusutan'],
            'id_frm_account_biaya_penyusutan'  => $pst['id_frm_account_biaya_penyusutan'],
            'name'            => $pst['name'],
            'note'            => $pst['note'],
            'update_by_users' =>  $this->session->userdata("id"),
        );
        $id_mrp_inventory_category = $this->global_models->update("mrp_inventory_category", array("id_mrp_inventory_category"=> $pst['id_detail']), $kirim);
      }
      else{
        $kirim = array(
            'id_frm_account'  => $pst['id_frm_account'],
            'id_frm_account_penyusutan'  => $pst['id_frm_account_penyusutan'],
            'id_frm_account_biaya_penyusutan'  => $pst['id_frm_account_biaya_penyusutan'],
            'name'            => $pst['name'],
            'note'            => $pst['note'],
            'create_by_users' =>  $this->session->userdata("id"),
            'create_date'     =>  date("Y-m-d H:i:s")
        );
        $id_mrp_inventory_category = $this->global_models->insert("mrp_inventory_category", $kirim);
      }
      if($id_mrp_inventory_category){
        $this->session->set_flashdata('success', 'Data tersimpan');
      }
      else{
        $this->session->set_flashdata('notice', 'Data gagal disimpan');
      }
      redirect("mrp/asset-category");
    }
    else{
      $data = $this->global_models->get("mrp_inventory_category", array("id_mrp_inventory_category" => $id_mrp_inventory_category));
      $this->template->build("add-category",
        array(
              'url'         => base_url()."themes/".DEFAULTTHEMES."/",
              'menu'        => 'mrp/asset-category',
              'title'       => lang("mrp_asset_category"),
              'detail'      => $data,
            ));
      $this->template
        ->set_layout('default')
        ->build("add-category");
    }
  }
  
	public function inventory(){
    $data = $this->global_models->get("mrp_inventory");
    $this->template->build("inventory",
      array(
            'url'         => base_url()."themes/".DEFAULTTHEMES."/",
            'menu'        => 'mrp/inventory',
            'title'       => lang("mrp_inventory"),
            'data'        => $data
          ));
    $this->template
      ->set_layout('default')
      ->build("inventory");
	}
  
	public function hutang_supplier($id_mrp_supplier){
    $data = $this->global_models->get_field("mrp_hutang_supplier", "count(id_mrp_hutang_supplier)", array("id_mrp_supplier" => $id_mrp_supplier));
    $supplier = $this->global_models->get("mrp_supplier", array("id_mrp_supplier" => $id_mrp_supplier));
    $this->template->build("hutang-supplier",
      array(
            'url'         => base_url()."themes/".DEFAULTTHEMES."/",
            'menu'        => 'mrp/supplier',
            'title'       => lang("mrp_hutang_supplier")." ".$supplier[0]->title,
            'total'       => $data,
            'id_mrp_supplier' => $id_mrp_supplier,
          ));
    $this->template
      ->set_layout('default')
      ->build("hutang-supplier");
	}
  
	public function supplier(){
    $data = $this->global_models->get("mrp_supplier");
    $this->template->build("supplier",
      array(
            'url'         => base_url()."themes/".DEFAULTTHEMES."/",
            'menu'        => 'mrp/supplier',
            'title'       => lang("mrp_supplier"),
            'data'        => $data
          ));
    $this->template
      ->set_layout('default')
      ->build("supplier");
	}
  
	public function brand(){
    $data = $this->global_models->get("mrp_inventory_brand");
    $this->template->build("brand",
      array(
            'url'         => base_url()."themes/".DEFAULTTHEMES."/",
            'menu'        => 'mrp/brand',
            'title'       => lang("mrp_brand"),
            'data'        => $data
          ));
    $this->template
      ->set_layout('default')
      ->build("brand");
	}
  
	public function asset_category(){
    $data = $this->global_models->get_query("
      SELECT A.*, B.title AS account
      FROM mrp_inventory_category AS A
      LEFT JOIN frm_account AS B ON A.id_frm_account = B.id_frm_account
      ");
    $this->template->build("category",
      array(
            'url'         => base_url()."themes/".DEFAULTTHEMES."/",
            'menu'        => 'mrp/asset-category',
            'title'       => lang("mrp_asset_category"),
            'data'        => $data
          ));
    $this->template
      ->set_layout('default')
      ->build("category");
	}
  
	public function bank(){
    $data = $this->global_models->get("mrp_bank");
    $this->template->build("bank",
      array(
            'url'         => base_url()."themes/".DEFAULTTHEMES."/",
            'menu'        => 'mrp/bank',
            'title'       => lang("mrp_bank"),
            'data'        => $data
          ));
    $this->template
      ->set_layout('default')
      ->build("bank");
	}
  
	function auto_inventory(){
    if (empty($_GET['term'])) exit ;
    $q = strtolower($_GET["term"]);
    if (get_magic_quotes_gpc()) $q = stripslashes($q);
    $items = $this->global_models->get_query("
      SELECT *
      FROM mrp_inventory
      WHERE LOWER(title) LIKE '%{$q}%'
      LIMIT 0,10
      ");
    foreach($items as $tms){
      $result[] = array(
          "id"    => $tms->id_mrp_inventory,
          "label" => $tms->title,
          "value" => $tms->title,
      );
    }
    echo json_encode($result);
    die;
  }
  
  public function ajax_hutang_supplier($id_mrp_supplier, $total = 0, $start = 0){
    
    $data = $this->global_models->get_query("
      SELECT A.*
      FROM mrp_hutang_supplier AS A
      WHERE A.id_mrp_supplier = '{$id_mrp_supplier}'
      ORDER BY A.tanggal DESC LIMIT {$start},10
      ");
    $total_debit = $this->global_models->get_query("
      SELECT SUM(A.price) AS saldo
      FROM mrp_hutang_supplier AS A
      WHERE A.id_mrp_supplier = '{$id_mrp_supplier}' AND A.pos = 1 AND A.tanggal <= '{$data[0]->tanggal}'
      ");
      
    $total_kredit = $this->global_models->get_query("
      SELECT SUM(A.price) AS saldo
      FROM mrp_hutang_supplier AS A
      WHERE A.id_mrp_supplier = '{$id_mrp_supplier}' AND A.pos = 2 AND A.tanggal <= '{$data[0]->tanggal}'
      ");
//    $this->debug($total_debit);
//    $this->debug($total_kredit, true);
//    $total_kredit = $this->global_models->get_field("frm_journal_log_account", "SUM(saldo)", 
//            array("id_frm_account" => $id_frm_account, "id_frm_journal" => $id_frm_journal, "pos" => 2, "tanggal >" => $data[0]->tanggal));
    
    $pos_asli = $this->global_models->get_field("frm_account", "pos", array("id_frm_account" => $id_frm_account));
    if($pos_asli == 1)
      $total_akhir = $total_debit[0]->saldo - $total_kredit[0]->saldo;
    else
      $total_akhir = $total_kredit[0]->saldo - $total_debit[0]->saldo;
    
    foreach ($data as $key => $value) {
      if($value->pos == 1){
        $saldo_debit = $value->price;
        $saldo_kredit = "";
        
      }
      else{
        $saldo_debit = "";
        $saldo_kredit = $value->price;
        
      }
      print "
      <tr class='ajax-quotation'>
        <td>{$value->tanggal}</td>
        <td>{$value->title}</td>
        <td>{$value->notransaksi}</td>
        <td style='text-align: right'>Rp ".  number_format($saldo_debit, 0, ",", ".")."</td>
        <td style='text-align: right'>Rp ".  number_format($saldo_kredit, 0, ",", ".")."</td>
        <td style='text-align: right; font-weight: bold;'>Rp ".  number_format($total_akhir, 0, ",", ".")."</td>
      </tr>";
      
      if($value->pos == 1){
        $total_akhir = $total_akhir + $saldo_debit;
      }
      else{
        $total_akhir = $total_akhir - $saldo_kredit;
      }
    }
    
    $this->load->library('pagination');

    $config['base_url'] = '';
    $config['total_rows'] = $total;
    $config['per_page'] = 10; 
    $config['uri_segment'] = 4; 
    $config['cur_tag_open'] = "<li class='active'><a href='javascript:void(0)'>"; 
    $config['cur_tag_close'] = "</a></li>"; 
    $config['first_tag_open'] = "<li>"; 
    $config['first_tag_close'] = "</li>"; 
    $config['last_tag_open'] = "<li>"; 
    $config['last_tag_close'] = "</li>"; 
    $config['next_tag_open'] = "<li>"; 
    $config['next_tag_close'] = "</li>"; 
    $config['prev_tag_open'] = "<li>"; 
    $config['prev_tag_close'] = "</li>"; 
    $config['num_tag_open'] = "<li>"; 
    $config['num_tag_close'] = "</li>";
    $config['function_js'] = "load_quotation";

    $this->pagination->initialize($config); 
    print "
    <tr class='ajax-quotation'>
      <td colspan='6'>
        <div class='pagination'>
          <ul>
          ".$this->pagination->create_links_ajax()."
          </ul>
        </div>
      </td>
    </tr>";
    die;
  }
  
  public function transaksi_hutang_supplier($id_mrp_supplier, $imrp_hutang_supplier = 0){
    if($this->input->post(NULL)){
      
      $pst = $this->input->post(NULL);
      if($pst['id_detail']){
        $kirim = array(
            "title"               => $pst['title'],
            "code"                => $pst['code'],
            "sort"                => $pst['sort'],
            "update_by_users"     => $this->session->userdata("id")
        );
        $id_keuangan_mahasiswa_jenis_biaya = $this->global_models->update("keuangan_mahasiswa_jenis_biaya", array("id_keuangan_mahasiswa_jenis_biaya" => $pst['id_detail']),$kirim);
      }
      else{
        $id_hutang_asset = $this->nbscache->get_explode("variable", "id_hutang_asset");
        $id_bayar_hutang_asset = $this->nbscache->get_explode("variable", "id_bayar_hutang_asset");
        
        $kirim = array(
            "id_mrp_supplier"     => $pst['id_mrp_supplier'],
            "title"               => $pst['title'],
            "notransaksi"         => $pst['notransaksi'],
            "tanggal"             => $pst['tanggal'],
            "price"               => $this->global_models->string_to_number($pst['price']),
            "pos"                 => 1,
            "note"                => $pst['note'],
            "create_by_users"     => $this->session->userdata("id"),
            "create_date"         => $pst['tanggal']
        );
        $id_mrp_hutang_supplier = $this->global_models->insert("mrp_hutang_supplier", $kirim);
        
//        $kirim = array(
//            "id_frm_journal"      => $this->mfrm->get_journal_current(),
//            "notransaksi"         => $pst['notransaksi'],
//            "saldo"               => $this->global_models->string_to_number($pst['price']),
//            "tanggal"             => $pst['tanggal'],
//            "note"                => "Transaksi Hutang Asset <a href='".site_url("mrp/hutang-supplier/".$pst['id_mrp_supplier'])."'>{$pst['notransaksi']}</a> -- ".$pst['note'],
//        );
//            
//        $id_frm_journal_log = $this->mfrm->create_journal_log($kirim);
//        if($id_frm_journal_log){
//            $debit[] = $this->global_models->string_to_number($pst['price']);
//            $debit_da[] = $id_hutang_asset[1];
//
//            $kredit[] = $this->global_models->string_to_number($pst['price']);
//            $kredit_da[] = $id_bayar_hutang_asset[1];
//            $this->mfrm->create_journal_log_account($this->mfrm->get_journal_current(), $id_frm_journal_log, $debit, $debit_da, $kredit, $kredit_da);
//          
//        }
      }
      if($id_mrp_hutang_supplier){
        $this->session->set_flashdata('success', 'Data tersimpan');
      }
      else{
        $this->session->set_flashdata('notice', 'Data tidak tersimpan');
      }
      redirect("mrp/hutang-supplier/".$pst['id_mrp_supplier']);
    }
    else{
      $detail = $this->global_models->get("mrp_hutang_supplier", array("id_mrp_hutang_supplier" => $id_mrp_hutang_supplier));
      $this->template->build("transaksi-hutang-supplier",
        array(
              'url'         => base_url()."themes/".DEFAULTTHEMES."/",
              'menu'        => 'mrp/supplier',
              'title'       => lang("mrp_transaksi_hutang_asset"),
              'detail'      => $detail,
              'id_mrp_supplier' => $id_mrp_supplier,
              'nim'         => $nim
            ));
      $this->template
        ->set_layout('default')
        ->build("transaksi-hutang-supplier");
    }
	}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */